/// 聊天消息模型（用于UI显示）
class ChatMessage {
  final String content;
  final bool isMe;
  final DateTime timestamp;
  final bool? isSending; // 是否正在发送
  final String? messageId; // 消息ID
  final String? fromUserId; // 发送者ID
  final String? toUserId; // 接收者ID
  final int messageType; // 消息类型：1-文本，2-图片，3-文件等
  final int status; // 消息状态：0-发送中，1-已发送，2-已读
  final String? fileUrl; // 文件/图片 URL
  final String? fileName; // 文件名
  final String? fileType; // 文件类型

  ChatMessage({
    required this.content,
    required this.isMe,
    required this.timestamp,
    this.isSending,
    this.messageId,
    this.fromUserId,
    this.toUserId,
    this.messageType = 1,
    this.status = 1,
    this.fileUrl,
    this.fileName,
    this.fileType,
  });

  Map<String, dynamic> toJson() {
    return {
      'content': content,
      'isMe': isMe,
      'timestamp': timestamp.toIso8601String(),
      'isSending': isSending,
      'messageId': messageId,
      'fromUserId': fromUserId,
      'toUserId': toUserId,
      'messageType': messageType,
      'status': status,
      'fileUrl': fileUrl,
      'fileName': fileName,
      'fileType': fileType,
    };
  }

  factory ChatMessage.fromJson(Map<String, dynamic> json) {
    return ChatMessage(
      content: json['content'] ?? '',
      isMe: json['isMe'] ?? false,
      timestamp: json['timestamp'] != null
          ? DateTime.parse(json['timestamp'])
          : DateTime.now(),
      isSending: json['isSending'],
      messageId: json['messageId'],
      fromUserId: json['fromUserId'],
      toUserId: json['toUserId'],
      messageType: json['messageType'] ?? 1,
      status: json['status'] ?? 1,
      fileUrl: json['fileUrl'],
      fileName: json['fileName'],
      fileType: json['fileType'],
    );
  }
}

